package cn.edu.ccut.refactor;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;

/**
 * 
 * @author Bai Yucheng 2008-4-16
 * 
 */
public class RenameSaturnModuleChange extends Change {
	private IJavaElement element;
	private String fSaturnNewName;

	public RenameSaturnModuleChange(IJavaElement ele, String newName) {
		element = ele;
		fSaturnNewName = newName;
	}

//	private IResource getResource() {
//		return ResourcesPlugin.getWorkspace().getRoot().findMember(fSaturnModulePath);
//	}

	public RefactoringStatus isValid(IProgressMonitor iprogressmonitor)
			throws CoreException {
		return null;
	}

	private String getDebugFileName(String s) {
		if (s.indexOf('.') == -1)
			return s.concat(".xml"); //$NON-NLS-1$
		else
			return s.substring(0, s.indexOf('.')) + ".xml"; //$NON-NLS-1$
	}

	public Change perform(IProgressMonitor iprogressmonitor)
			throws CoreException {
		IResource iresource = element.getResource();
        ((IFolder)iresource).move(createNewPath(), true, iprogressmonitor);
        return null;
	}

	 private IPath createNewPath() {
		return element.getResource().getFullPath().removeLastSegments(1)
				.append(fSaturnNewName);
	}
	
	@Override
	public String getName() {
		return Messages.getString("RenameSaturnModuleChange.2") + fSaturnNewName; //$NON-NLS-1$
	}

	@Override
	public void initializeValidationData(IProgressMonitor pm) {
	}

	public IJavaElement getElement() {
		return element;
	}

	public void setElement(IJavaElement element) {
		this.element = element;
	}

	@Override
	public Object getModifiedElement() {
		return element.getResource();
	}
}
